package day4;

class Monney{
    public int moeny;
    public String country;

    public Monney(int moeny, String country) {
        this.moeny = moeny;
        this.country = country;
    }
}

class Student implements Cloneable{
    public int age;
    public double score;
    public Monney money=new Monney(100,"china");

    public Student(int age, double score) {
        this.age = age;
        this.score = score;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student temp=new Student(this.age,this.score);
        temp.money.moeny=this.money.moeny;
        temp.money.country=this.money.country;
        return temp;
    }
//@Override
//protected Object clone() throws CloneNotSupportedException {
//    //调用Object类的克隆方法
//    return super.clone();
//}


}
public class demo1 {

    public static void main(String[] args) throws CloneNotSupportedException {
        Student stu1=new Student(20,99.5);
        //对stu1进行拷贝，注意拷贝方法的返回值是Object要进行向下转型
        Student stuX=(Student)stu1.clone();
        System.out.println("拷贝后两个对象中数据");
        System.out.println("stu1:"+stu1.money.moeny+" "+stu1.money.country);
        System.out.println("stu1:"+stu1.money.moeny+" "+stu1.money.country);

        System.out.println("拷贝后两个对象中数据");
        stuX.money.moeny=0;
        stuX.money.country="美国";
        System.out.println("stu1:"+stu1.money.moeny+" "+stu1.money.country);
        System.out.println("stu1:"+stuX.money.moeny+" "+stuX.money.country);
    }
}
